package com.yiwan.app.preventionsis.util;

import android.app.Application;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import com.app.tianwan.tianwanframe.utils.LogUtil;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;
import com.yiwan.app.preventionsis.config.GlobalCfg;
import com.yiwan.app.preventionsis.config.ImageResManager;

import java.io.File;

public class PreventionSis extends Application {
    private static PreventionSis instance;

    public static PreventionSis getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        instance = this;
        GlobalCfg.initConfig(this);
        ImageResManager.init(this);
        /**
         * 该Handler是在BroadcastReceiver中被调用，故
         * 如果需启动Activity，需添加Intent.FLAG_ACTIVITY_NEW_TASK
         * */
        UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler(){
            @Override
            public void dealWithCustomAction(Context context, UMessage msg) {
                LogUtil.d("dealWithCustomAction " + msg.after_open);
                UpdateManager updateManager = UpdateManager.getInstance(PreventionSis.this);
                updateManager.checkUpdateFromPush(context);
            }
        };
        PushAgent mPushAgent = PushAgent.getInstance(this);
        mPushAgent.setNotificationClickHandler(notificationClickHandler);
    }

    private BroadcastReceiver downloadReceiver;
    private long mDownloadId;

    public void registerDownloadReceiver(long downloadId){
        mDownloadId = downloadId;
        registerReceiver(downloadReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                LogUtil.d("onReceive()");
                DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
                long reference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
                LogUtil.d("Download onReceive(): " + manager.getUriForDownloadedFile(reference).getPath() + "; id = " + reference);
                if (mDownloadId == reference) {
                    if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
                        DownloadManager.Query query = new DownloadManager.Query().setFilterById(mDownloadId);
                        Cursor cursor = manager.query(query);
                        if (cursor != null) {
                            if(cursor.moveToFirst()){
                                int  fileNameIdx =
                                        cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);
                                String fileName = cursor.getString(fileNameIdx);
                                LogUtil.d("download file found, Name: " + fileName);
                                UpdateManager.getInstance(instance).installApk(context, new File(fileName));// 开始安装apk
                            }
                            cursor.close();
                        }
                    }
                }
                unregisterReceiver(downloadReceiver);
            }
        }, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }
}